<?php
	class order_administration extends BaseController{
		public $security_level = SECURITY_LEVEL_ADMIN;
			
		public function getData($in_params){
				
			return;
		}
		
		public function action_search($in_params = array()){
			general::debug("options", $in_params);
			general::debug("searchSet", isset($in_params['search_value']));
			$orders = new m_search_order_result();
			if(isset($in_params['search_value']) && $in_params['search_value'] != ""){
				$foundOrders = $orders->select("",array($in_params['search_options']=>$in_params['search_value']));
				general::debug("orders", $foundOrders);
				$this->template_data["foundOrders"] = $foundOrders;
				
			}
			else
			{
				$foundOrders = $orders->select();
				$this->template_data["foundOrders"] = $foundOrders;
			}
		
			return;
		}
		
		public function action_changeToPaid($in_params = array())
		{
			$order = new m_order();
			$orders = $order ->select("",array("id"=>$in_params["order"]));
			$order -> paid = 1;
			
			if ($order->update()){
				$RESULT["success"] = 1;
			}
			else
			{
				$RESULT["success"] = 0;
				$RESULT["message"] = "Nepodarilo sa zmenit stav objednavky";
			}
			return $RESULT;
		}
		
		public function action_addRowToPlan($in_params = array())
		{
			$RESULT["success"] = 0;
			$RESULT["message"] = "Nastal nejaký problém pri zmene plánu";
			if(isset($in_params['planId'])){
				$plan = new m_plan();
				
				if ($plan -> select("",array("id"=>$in_params["planId"]))){
					$plan->row++;
					$plan -> update();
					$RESULT["success"] = 1;
					$RESULT["message"] = "OK";
				}
			}
			return $RESULT;
		}
		
		public function action_order($in_params = array())
		{
			$notOrderedSeats = "";
			$RESULT["success"] = 0;
			$RESULT["message"] = "Neboli zadané žiadne údaje na objednávanie";
			if(isset($in_params['data'])){
				$order = new m_order();
				$order -> id =  time().$_SESSION["user"]["id"];
				$order -> paid = 0;
				$order -> user_id = $_SESSION["user"]["id"];
				$order -> created = time();
				if(!isset($in_params['orderForUser'])){
					$RESULT["message"] = "Zadajte meno používateľa pre ktorého robíte objednávku";
					$RESULT["success"] = 0;
				}
			    $order -> comment = $in_params['orderForUser'];
				
				$found = 0;
				foreach ($in_params['data'] as $value)
				{
					$planId = $value[0];
					$tableId = $value[1];
					$seatId = $value[2];
					
					$existSeat = new m_seat();
					if (!$existSeat -> select("",array("table"=>$tableId,"seat"=>$seatId,"plan_id"=>$planId)))
					{
						$found = 1;
						$seat = new m_seat();
						$seat -> table = $tableId;
						$seat -> seat = $seatId;
						$seat -> plan_id = $planId;
						$seat -> order_id = $order -> id;
						$seat -> insert();
					}
					else
					{
						$notOrderedSeats = $notOrderedSeats."p".$existSeat->plan_id."t".$existSeat->table."s".$existSeat->seat.";";
					}
					
				}
				if ($found == 1)
				{
					$order -> insert();
				}
				$RESULT["success"] = 1;
				if ($notOrderedSeats != "")
				{
					$RESULT["message"] = "Tieto miesta už sú obsadené: ".$notOrderedSeats;
				}
				else
				{
					$RESULT["message"] = "OK";
				}
				
			}	
			
			
			return $RESULT;
		}

		public function action_unreserveSeat($in_params = array())
		{
			$RESULT["success"] = 0;
			$RESULT["message"] = "Miesto nebolo nájdené";
			if(isset($in_params['planId'])&& isset($in_params['seatId']) && isset($in_params['tableId'])){
				$planId = $in_params['planId'];
				$tableId = $in_params['tableId'];
				$seatId = $in_params['seatId'];	
				$RESULT["params"] = $in_params;
				$existSeat = new m_seat();
				if ($existSeat -> select("",array("table"=>$tableId,"seat"=>$seatId,"plan_id"=>$planId)))
				{
					$existSeat -> delete();
					$RESULT["success"] = 1;
					$RESULT["message"] = "Ok";
				}
			}
			
			return $RESULT;
		}
		
		public function action_flush($in_params = array()){
			$memcache = new myMemcache();
			$memcache->flush();
			return "OK";
		}
		
	}
?>